package router

import (
	"gin/controller"
	"gin/middleware"
	"gin/queueAmqp"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/spf13/viper"
	"net/http"
)

func InitRouter() {
	router := gin.Default()

	// 加载静态模板
	//router.LoadHTMLGlob("./tpl/*")

	// 要在路由组之前全局使用「跨域中间件」, 否则OPTIONS会返回404
	router.Use(middleware.Cors())

	// 配置静态文件夹路径 第一个参数是api，第二个是文件夹路径
	router.StaticFS("/static", http.Dir("./static"))
	router.POST("/", controller.HealthyReturn)
	//router.POST("/test", controller.Test)

	dict := router.Group("dict")
	{
		dict.POST("/select", controller.DictSelect)
		dict.POST("/find", controller.DictFind)
		dict.POST("/create", controller.DictCreate)
		dict.POST("/update", controller.DictUpdate)
		dict.POST("/delete", controller.DictDelete)
		dict.POST("/pull", controller.DictPull)
		dict.POST("/sync_custom_tag", controller.DictAddByEntityCustomTags)
	}

	dictWord := router.Group("dict_word")
	{
		dictWord.POST("/select", controller.DictWordSelect)
		dictWord.POST("/find", controller.DictWordFind)
		dictWord.POST("/create", controller.DictWordCreate)
		dictWord.POST("/update", controller.DictWordUpdate)
		dictWord.POST("/delete", controller.DictWordDelete)
		dictWord.POST("/batch_update", controller.DictWordBatchUpdate)
		dictWord.POST("/batch_create", controller.DictWordBatchCreate)
	}

	wordSpeech := router.Group("word_speech")
	{
		wordSpeech.POST("/select", controller.WordSpeechSelect)
		wordSpeech.POST("/find", controller.WordSpeechFind)
		wordSpeech.POST("/create", controller.WordSpeechCreate)
		wordSpeech.POST("/update", controller.WordSpeechUpdate)
		wordSpeech.POST("/delete", controller.WordSpeechDelete)
	}

	sentence := router.Group("sentence")
	{
		sentence.POST("/select", controller.SentenceSelect)
		sentence.POST("/find", controller.SentenceFind)
		sentence.POST("/create", controller.SentenceCreate)
		sentence.POST("/update", controller.SentenceUpdate)
		sentence.POST("/delete", controller.SentenceDelete)
		sentence.POST("/lexical_analysis", controller.SentenceLexicalAnalysis)
		sentence.POST("/sentiment_analysis", controller.SentenceSentimentAnalysis)
		sentence.POST("/create_and_lexical_analysis", controller.SentenceAddAndLexicalAnalysis)
	}

	sentenceWord := router.Group("sentence_word")
	{
		sentenceWord.POST("/select", controller.SentenceWordSelect)
		sentenceWord.POST("/find", controller.SentenceWordFind)
		sentenceWord.POST("/create", controller.SentenceWordCreate)
		sentenceWord.POST("/update", controller.SentenceWordUpdate)
		sentenceWord.POST("/delete", controller.SentenceWordDelete)
		sentenceWord.POST("/similar_words", controller.SentenceWordSimilarWords)
	}

	sentenceWordSimilar := router.Group("word_similar")
	{
		sentenceWordSimilar.POST("/select", controller.WordSimilarSelect)
		sentenceWordSimilar.POST("/find", controller.WordSimilarFind)
		sentenceWordSimilar.POST("/create", controller.WordSimilarCreate)
		sentenceWordSimilar.POST("/update", controller.WordSimilarUpdate)
		sentenceWordSimilar.POST("/delete", controller.WordSimilarDelete)
		sentenceWordSimilar.POST("/query", controller.WordSimilarQuerySimilarWord)
	}

	sentenceNerToken := router.Group("sentence_ner_token")
	{
		sentenceNerToken.POST("/select", controller.SentenceNerTokenSelect)
		sentenceNerToken.POST("/find", controller.SentenceNerTokenFind)
		sentenceNerToken.POST("/create", controller.SentenceNerTokenCreate)
		sentenceNerToken.POST("/update", controller.SentenceNerTokenUpdate)
		sentenceNerToken.POST("/delete", controller.SentenceNerTokenDelete)
	}

	excel := router.Group("excel")
	{
		excel.POST("/import_data", controller.ExcelImportData)
		excel.POST("/format_paste_data", controller.ExcelFormatPasteData)
		excel.POST("/format_file_data", controller.ExcelFormatFileData)
	}

	media := router.Group("media")
	{
		// 上传文件
		media.POST("/upload", controller.UploadMedia)
		// 获取指定文件
		media.POST("/select", controller.SelectMediaByMd5Name)
		// 删除指定文件
		media.POST("/remove", controller.RemoveMediaByMd5Name)
	}

	story := router.Group("story")
	{
		story.POST("/select", controller.StorySelect)
		story.POST("/find", controller.StoryFind)
		story.POST("/create", controller.StoryCreate)
		story.POST("/update", controller.StoryUpdate)
		story.POST("/delete", controller.StoryDelete)
	}

	play := router.Group("play")
	{
		play.POST("/select", controller.PlaySelect)
		play.POST("/find", controller.PlayFind)
		play.POST("/create", controller.PlayCreate)
		play.POST("/update", controller.PlayUpdate)
		play.POST("/delete", controller.PlayDelete)
		play.POST("/upload_video", controller.PlayUploadVideo)
		play.POST("/video_list", controller.PlayVideoList)
	}

	event := router.Group("event")
	{
		event.POST("/select", controller.EventSelect)
		event.POST("/find", controller.EventFind)
		event.POST("/create", controller.EventCreate)
		event.POST("/update", controller.EventUpdate)
		event.POST("/delete", controller.EventDelete)
		event.POST("/analyzing_lines", controller.EventAnalyzingLines)
		event.POST("/update_by_filter", controller.EventUpdateByFilter)
	}

	role := router.Group("role")
	{
		role.POST("/select", controller.RoleSelect)
		role.POST("/find", controller.RoleFind)
		role.POST("/create", controller.RoleCreate)
		role.POST("/update", controller.RoleUpdate)
		role.POST("/delete", controller.RoleDelete)
	}

	scene := router.Group("scene")
	{
		scene.POST("/select", controller.SceneSelect)
		scene.POST("/find", controller.SceneFind)
		scene.POST("/create", controller.SceneCreate)
		scene.POST("/update", controller.SceneUpdate)
		scene.POST("/delete", controller.SceneDelete)
	}

	action := router.Group("action")
	{
		action.POST("/select", controller.ActionSelect)
		action.POST("/find", controller.ActionFind)
		action.POST("/create", controller.ActionCreate)
		action.POST("/update", controller.ActionUpdate)
		action.POST("/delete", controller.ActionDelete)
	}

	shot := router.Group("shot")
	{
		shot.POST("/select", controller.ShotSelect)
		shot.POST("/find", controller.ShotFind)
		shot.POST("/create", controller.ShotCreate)
		shot.POST("/update", controller.ShotUpdate)
		shot.POST("/delete", controller.ShotDelete)
	}

	light := router.Group("light")
	{
		light.POST("/select", controller.LightSelect)
		light.POST("/find", controller.LightFind)
		light.POST("/create", controller.LightCreate)
		light.POST("/update", controller.LightUpdate)
		light.POST("/delete", controller.LightDelete)
	}

	playRole := router.Group("play_role")
	{
		playRole.POST("/select", controller.PlayRoleSelect)
		playRole.POST("/find", controller.PlayRoleFind)
		playRole.POST("/create", controller.PlayRoleCreate)
		playRole.POST("/update", controller.PlayRoleUpdate)
		playRole.POST("/delete", controller.PlayRoleDelete)
	}

	roleGroup := router.Group("role_group")
	{
		roleGroup.POST("/select", controller.RoleGroupSelect)
		roleGroup.POST("/find", controller.RoleGroupFind)
		roleGroup.POST("/create", controller.RoleGroupCreate)
		roleGroup.POST("/update", controller.RoleGroupUpdate)
		roleGroup.POST("/delete", controller.RoleGroupDelete)
	}

	voice := router.Group("voice")
	{
		voice.POST("/transfer_to_text", controller.VoiceTransferToText)
	}

	task := router.Group("task")
	{
		task.POST("/ai_task_call_back", controller.AITaskCallBack)
	}

	port := viper.GetString("server.port")
	//声明不自动处理number类型 为float64
	binding.EnableDecoderUseNumber = true
	runQueue()
	if port != "" {
		panic(router.Run(":" + port))
	}
	panic(router.Run())
}

// 运行amqp队列得消费者
func runQueue() {
	// 运行转码队列
	for i := 1; i <= viper.GetInt("transcode.workNumber"); i++ {
		go queueAmqp.RunTransCodeConsumer()
	}
}
